package com.cattsoft.mobile.android.browser.javascript.run.lightdb;

import android.util.Log;

import com.cattsoft.mobile.android.browser.ProgressUtil;
import com.cattsoft.mobile.android.browser.javascript.run.JsExtRunner;
import com.cattsoft.mobile.android.browser.util.JsRunHelper;
import com.cattsoft.mobile.android.db.sqlite.DBInstance;
import com.cattsoft.mobile.android.pub.Constant;
import com.cattsoft.mobile.android.pub.StringUtil;

/**
 * 
 * Title: org.studio.crusoe <br>
 * Description: <br>
 * Date: 2010-12-29 <br>
 * Copyright (c) 2010 Crusoe Studio <br>
 * 
 * @author wusheng
 */
public class DBExecuteRunner extends JsExtRunner {
    private String sqlStatement;

    private String sucessCallBack;
    
    private String fatalCallBack;

    private DBInstance instance;

    public DBExecuteRunner(DBInstance instance, String sqlStatement, String sucessCallBack, String fatalCallBack) {
        super();
        this.instance = instance;
        this.sqlStatement = sqlStatement;
        this.sucessCallBack = sucessCallBack;
        this.fatalCallBack = fatalCallBack;
    }

    public void run() {
        try {
            instance.executeSQL(sqlStatement);
            if (!StringUtil.isBlank(sucessCallBack)) {
                JsRunHelper helper = new JsRunHelper();
                helper.appendJs(sucessCallBack + "();");
                this.webview.loadUrl(helper.prepareRun());
            }
        } catch (Throwable t) {
            if (!StringUtil.isBlank(fatalCallBack)) {
                JsRunHelper helper = new JsRunHelper();
                helper.appendJs(fatalCallBack + "(\"" + JsRunHelper.formatJsStringArg(t.toString()) + "\");");
                this.webview.loadUrl(helper.prepareRun());
            }
        } finally {
            ProgressUtil.close();
        }
    }

}
